const express = require('express')
const Router = express.Router()
const model = require('../model/model')


const Base = model.getModel('base')

Router.post('/save',(req,res)=>{
    console.log(req.body,'req.body')
    let data = {...req.body,createTime:Date.now()}
    const baseData = new Base(data)
    baseData.save((e,d)=>{
        if(e){
            return res.json({code:1,msg:'服务出错了！！！'})
        }
        return res.json({code:200,msg:'添加成功'})
    })
})
Router.post('/list',async (req,res)=>{
    let { page = 1, limit = 10, wentiData1 } = req.body;
    page = parseInt(page); // 将字符串参数转换为整数
    limit = parseInt(limit);
    const query = {};

    // 如果传递了 gender 参数，则添加性别条件
    if (wentiData1) {
        query.wentiData1 = wentiData1;
    }
    try {
        // 使用 Mongoose 进行分页查询
        const items = await Base.find(query)
          .skip((page - 1) * limit) // 跳过的文档数
          .limit(limit);
           // 限制查询数量
    
        // 查询总文档数
        console.log(items,'items')
        const totalItems = await Base.countDocuments(query);
        res.json({
          totalItems,
          totalPages: Math.ceil(totalItems / limit),
          currentPage: page,
          list:items
        });
      } catch (error) {
        res.status(500).json({ message: 'Server Error', error });
      }

})
Router.get('/test',(req,res)=>{
    return res.json({code:200,msg:'成功了'})
})

module.exports = Router